﻿using AdvancedPoll.Models;
using AdvancedPoll.Services;
using AdvancedPoll.ViewModels;
using Orchard.ContentManagement.Drivers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Web;

namespace AdvancedPoll.Drivers
{
    [ComVisible(false)]
    public class PollPartDriver : ContentPartDriver<PollPart>
    {
        private readonly IPollService _pollService;

        public PollPartDriver(IPollService pollService)
        {
            _pollService = pollService;
        }

        protected override DriverResult Display(PollPart part, string displayType, dynamic shapeHelper)
        {
            return Combined(
                ContentShape("Parts_Poll",
                    () => shapeHelper.Parts_Poll(
                    Id: part.Id,
                    Question: part.Question,
                    OpenDateUtc: part.OpenDateUtc,
                    CloseDateUtc: part.CloseDateUtc,
                    MaxVotes: part.MaxVotes,
                    IsShown: part.IsShown,
                    IsOpen: !_pollService.IsPollClosed(part.Id),
                    Choices: _pollService.GetChoices(part.Id).ToList())),
                ContentShape("Parts_Poll_Admin_Summary",
                    () => shapeHelper.Parts_Poll_Admin_Summary(
                        Question: part.Question,
                        ChoiceNum: _pollService.GetChoices(part.Id).Count()
                        )),
                ContentShape("Parts_Poll_Result",
                    () => shapeHelper.Parts_Poll_Result(
                        Question: part.Question,
                        Choices: _pollService.GetPollWithResult(part.Id).Choices,
                        Shown: part.IsShown
                        ))
                );
        }

        // GET
        protected override DriverResult Editor(PollPart part, dynamic shapeHelper)
        {
            if (part == null)
                throw new ArgumentNullException("part");
            var choices = _pollService
                            .GetChoices(part.Id)
                            .Select(c => new ChoiceEntry { Choice = c, Action = "Alter" })
                            .ToList();
            var model = new PollViewModel(part, choices);
            return ContentShape("Parts_Poll_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Poll",
                    Model: model,
                    Prefix: Prefix));
        }

        // POST
        protected override DriverResult Editor(
                    PollPart part,
                    Orchard.ContentManagement.IUpdateModel updater,
                    dynamic shapeHelper)
        {
            if (updater == null)
                throw new ArgumentNullException("updater");
            if (part == null)
                throw new ArgumentNullException("part");
            var model = new PollViewModel();
            model.Choices = new List<ChoiceEntry>();

            updater.TryUpdateModel(model, Prefix, null, null);

            _pollService.EditPoll(part, model);

            return Editor(part, shapeHelper);
        }
    }
}